package online.foxnull2.foxmall.cart.vo.cart;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

@Data
@Accessors(chain = true)
public class CartVo implements Serializable {


    private List<CartItem> items;
    private BigDecimal reduce = new BigDecimal("0.00");
    /**
     * private Integer countNum;
     * private Integer countType;
     * private BigDecimal totalAmount;
     */

    public Integer getCount() {
        if (!ObjectUtils.isEmpty(getItems())) {
            Integer countNum = 0;
            for (CartItem item : getItems()) {
                countNum += item.getCount();
            }
            return countNum;
        }
        return null;
    }

    public Integer getCountType() {
        if (!ObjectUtils.isEmpty(getItems())) {
            return getItems().size();
        }
        return null;
    }

    public BigDecimal getTotalAmount() {
        if (!ObjectUtils.isEmpty(getItems())) {
            BigDecimal totalAmount = new BigDecimal("0.00");
            for (CartItem item : getItems()) {
                if (Boolean.TRUE.equals(item.getCheck())) {
                    totalAmount = totalAmount.add(item.getTotalPrice());
                }
            }
            totalAmount = totalAmount.subtract(getReduce());
            return totalAmount;
        }
        return null;
    }
}
